package com.snap.vseries.analyze;


import com.snap.vseries.bean.ChannelBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CtValueLinear {

    // TODO: 5/14/21 如果交点处于最后一个循环的前一个循环内，这个方法算不出的
    public static void doAnalyze(List<ChannelBean> channelBeans, AlgoParams algoParams) {
        for (int j = 0; j < channelBeans.size(); j++) {
            ChannelBean bean = channelBeans.get(j);
            if (bean.getFit() == null) continue;
            if (bean.getFit().length < 3) continue;

            List<Integer> meetList = new ArrayList<>();
            for (int i = 0; i < bean.getFit().length - 2; i++) {
                if (bean.getFit()[i] <= bean.getThreshold()//小于阈值  且后面2个数都大于阈值
                        && bean.getFit()[i + 1] >= bean.getThreshold()
                        && bean.getFit()[i + 2] >= bean.getThreshold()) {
                    meetList.add(i);
                }
            }
            if (meetList.size() > 0) {
                int ctNear = Collections.max(meetList);//含有多个  取大的那个
                float ctValue = ctNear + 1 + Math.abs((bean.getThreshold() - bean.getFit()[ctNear]) / (bean.getFit()[ctNear] - bean.getFit()[ctNear + 1]));
                BigDecimal decimal = new BigDecimal(ctValue);
                ctValue = decimal.setScale(3, BigDecimal.ROUND_HALF_UP).floatValue();
                bean.setCtValue(ctValue);
            }
        }
    }
}
